﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using TeamDeployer.TeamFoundation.Build.Workflow.CustomTypes;

namespace TeamDeployer.TeamFoundation.Build.Workflow.Design
{
    public partial class DialogDeploySpecList : Form
    {
        // Fields
        private List<DeploySpec> _SpecList;

        public DialogDeploySpecList(List<DeploySpec> specs)
        {
            this.components = null;
            this.InitializeComponent();
            this.SpecList = specs;
            this.SetButtonState();
        }

        public List<DeploySpec> SpecList
        {
            get
            {
                return this._SpecList;
            }
            set
            {
                this._SpecList = value;
                this.BindListView();
            }
        }
        // Properties
        private DeploySpec SelectedSpec
        {
            get
            {
                if (this.PackageListView.SelectedItems.Count == 0)
                {
                    return null;
                }
                ListViewItem item = this.PackageListView.SelectedItems[0];
                return this.SpecList.SingleOrDefault<DeploySpec>(
                    s => (s.Id == ((Guid)item.Tag)));
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            using (DialogDeploySpec dialog = new DialogDeploySpec())
            {
                if (dialog.ShowDialog(this) == DialogResult.OK)
                {
                    this.AddNewSpec(dialog.Spec);
                    this.SetButtonState();
                }
            }
        }

        private void AddNewSpec(DeploySpec spec)
        {
            this.SpecList.Add(spec);
            this.PackageListView.Items.Add(spec.ToListViewItem());
        }

        private void BindListView()
        {
            this.PackageListView.Items.Clear();
            foreach (DeploySpec spec in this.SpecList)
            {
                this.PackageListView.Items.Add(spec.ToListViewItem());
            }
            this.SetButtonState();
        }

        private void EditButton_Click(object sender, EventArgs e)
        {
            if (this.PackageListView.SelectedItems.Count != 0)
            {
                int num = this.PackageListView.SelectedIndices[0];
                DeploySpec selectedSpec = this.SelectedSpec;
                using (DialogDeploySpec dialog = new DialogDeploySpec())
                {
                    dialog.Spec = selectedSpec;
                    if (dialog.ShowDialog(this) == DialogResult.OK)
                    {
                        this.PackageListView.Items[num] = dialog.Spec.ToListViewItem();
                        this.SetButtonState();
                    }
                }
            }
        }

        private int FindSpecRowIndex(Guid id)
        {
            for (int i = 0; i < this.PackageListView.Items.Count; i++)
            {
                ListViewItem item = this.PackageListView.Items[i];
                if (((Guid)item.Tag) == id)
                {
                    return i;
                }
            }
            return -1;
        }

        private void PackageListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SetButtonState();
        }

        private void RemoveButton_Click(object sender, EventArgs e)
        {
            if ((this.PackageListView.SelectedItems.Count != 0) && (this.SelectedSpec != null))
            {
                DeploySpec selectedSpec = this.SelectedSpec;
                int index = this.FindSpecRowIndex(selectedSpec.Id);
                if (index >= 0)
                {
                    this.PackageListView.Items.RemoveAt(index);
                }
                this.SpecList.Remove(selectedSpec);
                if (this.PackageListView.Items.Count > 0)
                {
                    this.PackageListView.Items[this.PackageListView.Items.Count - 1].Selected = true;
                }
                this.SetButtonState();
            }
        }

        private void SetButtonState()
        {
            int num = (this.PackageListView.SelectedIndices.Count == 0) ? -1 :
                      this.PackageListView.SelectedIndices[0];
            this.RemoveButton.Enabled = num >= 0;
            this.EditButton.Enabled = num >= 0;
        }
    }
}